<?php
/**
 * Each engineer has a duty to keep the code elegant
 * Created by wolffy.
 * Author @wolffy<13750088090@sina.cn>
 * Date: 2018/8/12
 * Time: 11:40
 */
namespace App\Repositories;

use App\Models\UserProfit;
use Illuminate\Support\Facades\DB;

class UserProfitRepository
{
    /**
     * 创建用户收益
     * @param array $params
     * @return mixed
     */
    public function create(array $params)
    {
        return UserProfit::create($params);
    }

    /**
     * 创建用户收益
     * @param array $params
     * @return mixed
     */
    public function insertGetId(array $params)
    {
        return UserProfit::insertGetId($params);
    }

    /**
     * 查找一条收益记录
     * @param $id
     * @return mixed
     */
    public function byId($id)
    {
        return UserProfit::find($id);
    }

    /**
     * 统计所有收益
     * @param $userId
     * @return mixed
     */
    public function profitAll($userId)
    {
        $sum = UserProfit::where('to_user_id', $userId)->sum('profit_money');
        return sprintf("%.2f", $sum);
    }

    public function profitAllToUser($fromUserId, $toUserId)
    {
        $sum = UserProfit::where([
            'from_user_id' => $fromUserId,
            'to_user_id' => $toUserId
        ])->sum('profit_money');
        return sprintf("%.2f", $sum);
    }

    /**
     * 根据收益类型
     * @param $userId
     * @param $profitType
     * @return mixed
     */
    public function profitFromTypeSum($userId, $profitType)
    {
        $sum = UserProfit::where([
            'to_user_id' => $userId,
            'profit_type' => $profitType
        ])->sum('profit_money');
        return sprintf("%.2f", $sum);
    }

    public function profitFromTypeCount($userId, $profitType)
    {
        $count = UserProfit::where([
            'to_user_id' => $userId,
            'profit_type' => $profitType
        ])->count();
        return (string)$count;
    }

    public function profitFromTypeToday($userId, $profitType = [])
    {
        $startTime = mktime(0,0,0,date('m'),date('d'),date('y'));
        $endTime = time();
        $profitType = (!empty($profitType)) ? [$profitType] : [1,2,3,4,5,6];
        $sum = UserProfit::where([
            'to_user_id' => $userId,
        ])
            ->whereIn('profit_type', $profitType)
            ->whereBetween('create_time', [$startTime, $endTime])
            ->sum('profit_money');
        return sprintf("%.2f", $sum);
    }

    public function profitFromTypeYesterday($userId, $profitType = [])
    {
        $startTime = mktime(0,0,0,date('m'),date('d')-1,date('Y'));
        $endTime = mktime(0,0,0,date('m'),date('d'),date('Y'))-1;
        $profitType = (!empty($profitType)) ? [$profitType] : [1,2,3,4,5,6];
        $sum = UserProfit::where([
            'to_user_id' => $userId,
        ])
            ->whereIn('profit_type', $profitType)
            ->whereBetween('create_time', [$startTime, $endTime])
            ->sum('profit_money');
        return sprintf("%.2f", $sum);
    }

    public function profitFromTypeThisMonth($userId, $profitType = [])
    {
        $startTime = mktime(0,0,0,date('m'),1,date('Y'));
        $endTime = mktime(23,59,59,date('m'),date('t'),date('Y'));
        $profitType = (!empty($profitType)) ? [$profitType] : [1,2,3,4,5,6];
        $sum = UserProfit::where([
            'to_user_id' => $userId,
        ])
            ->whereIn('profit_type', $profitType)
            ->whereBetween('create_time', [$startTime, $endTime])
            ->sum('profit_money');
        return sprintf("%.2f", $sum);
    }

    public function groupByMonthProfit($userId, $profitType)
    {
        return UserProfit::select(DB::raw('sum(profit_money) as profit_money, create_month'))
            ->groupBy('create_month')
            ->where([
            'to_user_id' => $userId,
            'profit_type' => $profitType,
        ])
            ->orderBy('create_month', 'desc')
            ->get()->toArray();
    }

    public function byMonthAll($userId, $profitType, $createMonth)
    {
        return UserProfit::where([
            'to_user_id' => $userId,
            'profit_type' => $profitType,
            'create_month' => $createMonth,
        ])
            ->orderBy('create_time', 'desc')
            ->get()->toArray();
    }

    public function profitGroupAll($userIds)
    {
        $sum = UserProfit::whereIn('to_user_id', $userIds)
            ->sum('profit_money');
        return sprintf("%.2f", $sum);
    }

    public function profitByCreateTime($userIds, $startTime, $endTime)
    {
        $sum = UserProfit::whereIn('to_user_id', $userIds)
            ->whereBetween('create_time', [$startTime, $endTime])
            ->sum('profit_money');
        return sprintf("%.2f", $sum);
    }

}